// components/pagination/index.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        url: {type: String, value: ''},
        
        extClass: {type: String, value: ''}
    },

    /**
     * 组件的初始数据
     */
    data: {
        page: 1,
        limit: 20,
        dataList: []
    },
    pageLifetimes: {
        show: function() {
          // 页面被展示
        //   if (this.data.url && wx.getStorageSync('szm_token')) {
        //     //   this.getPageData({});
        //   }
        },
        hide: function() {
          // 页面被隐藏
          this.setData({dataList: []})
        },
        resize: function(size) {
          // 页面尺寸变化
        }
      },
    lifetimes: {
        // attached() {
        //     this.getPageData();
        // }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        refresher() {
            this.data.page = 1;
            this.setData({
                showNoMore: false
            })
            setTimeout(() => {
                console.log('====> refresher')
                this.getPageData({});
            }, 1000);
        },
    
        upper(e) {
            console.log('====> upper')
            // this.data.page = 1;
            // this.setData({showNoMore: false})
            // this.getPageData({});
          },
        
        lower(e) {
          console.log('====> lower')
            const {page, limit, total} = this.data;
            if (page * limit < total) {
                ++this.data.page
                this.getPageData({});
            }
        },
    
        scroll(e) {
        // console.log(e)
        },

        getPageData({url, data}) {
            
            url && this.setData({path: url})
            data && this.setData({params: data || {}})
            
            const {page, limit, dataList, params, path} = this.data;
            wx.$http.get({url: url || path, data: Object.assign(data || params, {page, limit})}).then(res => {
                const {total, list} = res.data
                this.setData({
                    total,
                    closeRefresher: false
                })
                if (page * limit >= total) {
                    this.setData({
                        showNoMore: true
                    })
                }
                if(page === 1 || list.length > 0) {
                  this.triggerEvent("pageList", {list, page})
                }

            }).catch(() => {
                this.setData({closeRefresher: false})
            });
        },
    }
})
